package mongo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.types.ObjectId;

import tool.DateUtils;
import vo.Item;
import vo.TopicItem;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class TopicItemDAO {
	static Mongo m;
	static DB db;
	static DBCollection coll;

	public static void main(String[] args) throws Exception {
		String startDate = null;
		String endDate = null;
		String sourceId = null;
		int pageSize = 8;
		int totalPage = 0;
		String topicId = "7";
		int curPage = 1;
		try {
			System.out.println("检索条件：" + startDate + "," + endDate + "," + sourceId + "," + topicId);
			int totalCount = TopicItemDAO.getTotalCountByCondition(startDate, endDate, sourceId, topicId);
			System.out.println("totalCount:" + totalCount);
			if (totalCount % pageSize == 0)
				totalPage = totalCount / pageSize;
			else
				totalPage = totalCount / pageSize + 1;
			System.out.println("totalPage:" + totalPage);
			List<TopicItem> tis = TopicItemDAO.getAllByCondition((curPage - 1) * pageSize, pageSize, startDate, endDate, sourceId, topicId);
			System.out.println("找到多少个:" + tis.size());
			for (TopicItem ti : tis) {
				String itemId = ti.getItemId();
				Item item = ItemDAO.getById(itemId);
				System.out.println("item:" + item);
			}

			Item item = ItemDAO.getById("5019b216f7b1f3c0ef4d0127");
			System.out.println("item:" + item);
			System.out.println("item:" + item.getContent());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static DBCollection getDBCollection(String collectionName) throws Exception {
		if (m == null)
			m = new Mongo("182.50.3.54", 27017);
		if (db == null)
			db = m.getDB("yuqing");
		if (coll == null)
			coll = db.getCollection(collectionName);
		return coll;
	}

	public static List<TopicItem> getAllItems(int start, int pageSize) throws Exception {
		List<TopicItem> tis = new ArrayList<TopicItem>();
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		DBCursor cursor = coll.find(obj).skip(start).limit(pageSize);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			String topicId = (String) it.get("topicId");
			String itemId = (String) it.get("itemId");

			TopicItem ti = new TopicItem();
			ti.setItemId(itemId);
			ti.setTopicId(Integer.parseInt(topicId));
			tis.add(ti);
		}
		cursor.close();
		return tis;
	}

	public static int getCountByTopicNoTimeRange(String topicNo, Date startDate, Date endDate) throws Exception {
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		obj.put("pubtime", new BasicDBObject("$gte", startDate).append("$lte", endDate));
		obj.put("topicId", topicNo);// mongo topicId=mysql topicno
		DBCursor cursor = coll.find(obj).sort(new BasicDBObject().append("replynum", -1));
		int result = cursor.count();
		cursor.close();
		return result;
	}

	// 查询topicId,某时间范围内，按照reply排序
	public static List<TopicItem> getByTopicNoTimeRange(String topicNo, Date startDate, Date endDate) throws Exception {
		List<TopicItem> tis = new ArrayList<TopicItem>();
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		obj.put("pubtime", new BasicDBObject("$gte", startDate).append("$lte", endDate));
		obj.put("topicId", topicNo);// mongo topicId=mysql topicno
		// DBCursor cursor = coll.find(obj).sort(new
		// BasicDBObject().append("replynum", -1)).skip(0).limit(10);
		DBCursor cursor = coll.find(obj).sort(new BasicDBObject());
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			String itemId = (String) it.get("itemId");
			Date pubTime = (Date) it.get("pubtime");
			String source = (String) it.get("source");
			String type = (String) it.get("type");

			TopicItem ti = new TopicItem();
			ti.setItemId(itemId);
			ti.setTopicId(Integer.parseInt(topicNo));
			ti.setPubTime(pubTime);
			ti.setSource(source);
			ti.setType(type);
			tis.add(ti);
		}
		cursor.close();
		return tis;
	}

	public static List<TopicItem> getByTopicNosTimeRange(ArrayList<String> topicIds, Date startDate, Date endDate) throws Exception {
		List<TopicItem> tis = new ArrayList<TopicItem>();
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		BasicDBList values = new BasicDBList();
		for (String topicNo : topicIds) {
			values.add(topicNo);
		}
		obj.put("topicId", new BasicDBObject("$in", values));
		obj.put("pubtime", new BasicDBObject("$gte", startDate).append("$lte", endDate));

		DBCursor cursor = coll.find(obj).sort(new BasicDBObject().append("replynum", -1)).skip(0).limit(10);
		// DBCursor cursor = coll.find(obj).sort(new BasicDBObject());
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			String itemId = (String) it.get("itemId");
			Date pubTime = (Date) it.get("pubtime");
			String source = (String) it.get("source");
			String type = (String) it.get("type");
			String topicNo = (String) it.get("topicId");
			String frag = (String) it.get("frag");

			TopicItem ti = new TopicItem();
			ti.setItemId(itemId);
			ti.setTopicId(Integer.parseInt(topicNo));
			ti.setPubTime(pubTime);
			ti.setSource(source);
			ti.setType(type);
			ti.setFrag(frag);
			tis.add(ti);
		}
		cursor.close();
		return tis;
	}

	public static List<TopicItem> getByTopicNo(String topicNo) throws Exception {
		List<TopicItem> tis = new ArrayList<TopicItem>();
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		obj.put("topicId", topicNo);// mongo topicId=mysql topicno
		DBCursor cursor = coll.find(obj);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			String itemId = (String) it.get("itemId");
			Date pubTime = (Date) it.get("pubtime");
			String source = (String) it.get("source");
			String type = (String) it.get("type");

			TopicItem ti = new TopicItem();
			ti.setItemId(itemId);
			ti.setTopicId(Integer.parseInt(topicNo));
			ti.setPubTime(pubTime);
			ti.setSource(source);
			ti.setType(type);
			tis.add(ti);
		}
		cursor.close();
		return tis;
	}

	public static List<TopicItem> getBySource(String source) throws Exception {
		List<TopicItem> tis = new ArrayList<TopicItem>();
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		obj.put("source", source);
		DBCursor cursor = coll.find(obj);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			String itemId = (String) it.get("itemId");
			Date pubTime = (Date) it.get("pubtime");
			String type = (String) it.get("type");
			String topicId = (String) it.get("topicId");

			TopicItem ti = new TopicItem();
			ti.setItemId(itemId);
			ti.setTopicId(Integer.parseInt(topicId));
			ti.setPubTime(pubTime);
			ti.setSource(source);
			ti.setType(type);
			tis.add(ti);
		}
		cursor.close();
		return tis;
	}

	public static int getCountBySource(String source) throws Exception {
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		obj.put("source", source);
		DBCursor cursor = coll.find(obj);
		int result = cursor.size();
		cursor.close();
		return result;
	}

	public static int getCountByTopicIdSourceId(String topicId, String sourceId) throws Exception {
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		obj.put("source", sourceId);
		obj.put("topicId", topicId);
		DBCursor cursor = coll.find(obj);
		int result = cursor.size();
		cursor.close();
		return result;
	}

	public static List<TopicItem> getByItemId(String itemId) throws Exception {
		List<TopicItem> tis = new ArrayList<TopicItem>();
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		obj.put("itemId", itemId);
		DBCursor cursor = coll.find(obj);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			String topicId = (String) it.get("topicId");
			TopicItem ti = new TopicItem();

			ti.setItemId(itemId);
			ti.setTopicId(Integer.parseInt(topicId));
			tis.add(ti);
		}
		cursor.close();
		return tis;
	}

	public static TopicItem getByTopicIdItemId(String topicId, String itemId) throws Exception {
		TopicItem ti = new TopicItem();
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		obj.put("topicId", topicId);
		obj.put("itemId", itemId);
		DBCursor cursor = coll.find(obj);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			String frag = (String) it.get("frag");
			Date pubTime = (Date) it.get("pubTime");
			Integer replyNum = (Integer) it.get("replynum");
			ti.setItemId(itemId);
			ti.setFrag(frag);
			ti.setTopicId(Integer.parseInt(topicId));
			ti.setPubTime(pubTime);
			ti.setReplyNum(replyNum);
		}
		cursor.close();
		return ti;
	}

	public static boolean isExiste(String itemId, String topicId) throws Exception {
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		obj.put("itemId", itemId);
		obj.put("topicId", topicId);
		DBCursor cursor = coll.find(obj);
		if (cursor.hasNext())
			return true;
		cursor.close();
		return false;
	}

	public static int getTotalCount() throws Exception {
		DBCollection coll = getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		DBCursor cursor = coll.find(obj);
		int result = cursor.size();
		cursor.close();
		return result;
	}

	public static int getCount(String topicId) throws Exception {
		DBCollection coll = getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		obj.put("topicId", topicId);
		DBCursor cursor = coll.find(obj);
		int result = cursor.size();
		cursor.close();
		return result;
	}

	public static int getTotalCountByCondition(String startDate, String endDate, String sourceId, String topicId) throws Exception {
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();
		DBCursor cursor = coll.find(obj);

		// 时间检索条件
		if (startDate != null && startDate.length() != 0)
			if (endDate != null && endDate.length() != 0)
				obj.put("pubtime", new BasicDBObject("$gte", DateUtils.str2Date(startDate + " 00:00:00")).append("$lte", DateUtils.str2Date(endDate + " 00:00:00")));

		// source检索条件
		if (sourceId != null)
			obj.put("source", sourceId);

		// topic检索条件
		if (topicId != null)
			obj.put("topicId", topicId);

		int result = cursor.size();
		cursor.close();
		return result;
	}

	public static List<TopicItem> getAllByCondition(int start, int pageSize, String startDate, String endDate, String sourceId, String topicId) throws Exception {
		List<TopicItem> topicItems = new ArrayList<TopicItem>();
		DBCollection coll = TopicItemDAO.getDBCollection("topicItem");
		BasicDBObject obj = new BasicDBObject();

		// 时间检索条件
		if (startDate != null && endDate != null)
			obj.put("pubtime", new BasicDBObject("$gte", DateUtils.str2Date(startDate + " 00:00:00")).append("$lte", DateUtils.str2Date(endDate + " 00:00:00")));

		// source检索条件
		if (sourceId != null)
			obj.put("source", sourceId);

		// topic检索条件
		if (topicId != null)
			obj.put("topicId", topicId);

		DBCursor cursor = coll.find(obj).skip(start).limit(pageSize);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			ObjectId id = (ObjectId) it.get("_id");
			String itemId = (String) it.get("itemId");
			Date pubTime = (Date) it.get("pubtime");
			String type = (String) it.get("type");
			String frag = (String) it.get("frag");
			String source = (String) it.get("source");
			Integer replyNum = (Integer) it.get("replynum");
			String dbTopicId = (String) it.get("topicId");
			// Integer num = (Integer) it.get("num");//TODO：忘了包装了

			TopicItem topicItem = new TopicItem();
			topicItem.setFrag(frag);
			topicItem.setId(id.toString());
			topicItem.setItemId(itemId);
			topicItem.setPubTime(pubTime);
			topicItem.setReplyNum(replyNum);
			topicItem.setSource(source);
			topicItem.setTopicId(Integer.parseInt(dbTopicId));
			topicItem.setType(type);
			topicItems.add(topicItem);
		}
		cursor.close();
		return topicItems;
	}

}